<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<title></title>
	[#include "/admin/include/admin_static.htm"]
</head>
<body>
<div class="mini-toolbar" style="border-bottom:0;padding:0 0 2px;">
	<table style="width:100%;">
		<tr>
			<td style="width:100%;"><a class="mini-menubutton"
				iconCls="icon2-drawer" plain="true" menu="#popupMenu">文件</a> <span
				class="separator"></span> <a class="mini-button" iconCls="icon2-add"
				plain="true" onclick="onNew" tooltip="新建">新建</a> <a
				class="mini-button" iconCls="icon2-edit" plain="true"
				onclick="onEdit" tooltip="编辑">编辑</a> <a class="mini-button"
				iconCls="icon2-del" plain="true" onclick="onDelete" tooltip="删除">删除</a>
				<span class="separator"></span> <a class="mini-button"
				iconCls="icon2-node" plain="true" onclick="onBindNode"
				tooltip="修改权限">修改权限</a> <!--menu-->
				<ul id="popupMenu" class="mini-menu" style="display:none;">
					<li><span>操作</span>
						<ul>
							<li iconCls="icon2-add" onclick="onNew">新建</li>
							<li class="separator"></li>
							<li iconCls="icon2-edit" onclick="onEdit">编辑</li>
							<li iconCls="icon2-del" onclick="onDelete">删除</li>
						</ul></li>
					<li class="separator"></li>
					<li iconCls="icon2-reload" onclick="CommonReloadCurTab">刷新</li>
					<li class="separator"></li>

				</ul></td>
		</tr>
	</table>
</div>
<div class="mini-fit" style="width:100%;height:100%;">
	<div class="mini-splitter" style="width:100%;height:100%;"
		allowResize="false">
		<div showCollapseButton="false">
			<div id="treegrid1" class="mini-treegrid"
				style="width:100%;height:100%;" borderStyle="border:none"
				url="${path}/admin/rbac/role_getDataJson.aspx" showTreeIcon="true"
				treeColumn="title" expandOnLoad="true" resultAsTree="false"
				checkRecursive="true" sortMode="client" allowMoveColumn="false"
				fitColumns="false" ondrawcell="onTreegrid1DrawCell"
				onrowdblclick="onTreegrid1RowDblClick">
				<div property="columns">
					<div field="id" width="60" headerAlign="center" align="center"
						allowSort="true">ID</div>
					<div name="title" field="title" width="250" headerAlign="center"
						allowSort="true">名称</div>
					<div field="pid" width="60" headerAlign="center" align="center"
						allowSort="true">父ID</div>
					<div field="level" width="60" headerAlign="center" align="center"
						allowSort="true">层级</div>
				</div>
			</div>
		</div>
		<div size="300" showCollapseButton="false">
			<div id="panel1" class="mini-panel" title="属性窗口" enabled="false"
				iconCls="icon2-property" showFooter="true"
				style="width:100%;height:100%;" borderStyle="border:none">
				<!--body-->
				<form id="form1" style="width:100%;height:100%;">
					<table class="dui_table w100p">
						<tr>
							<td class="w80"><span title="id">ID</span>：</td>
							<td class="w100p"><input name="id" class="mini-textbox"
								enabled="false" vtype="int" style="width:100%;" />
							</td>
						</tr>
						<tr>
							<td><span title="pid">父ID</span>：</td>
							<td><input id="pid" name="pid" class="mini-textbox"
								vtype="int" required="true" style="width:100%;" value="0" />
							</td>
						</tr>
						<tr>
							<td><span title="title">名称</span>：</td>
							<td><input name="title" class="mini-textbox"
								vtype="rangeLength:1,100" required="true" style="width:100%;" />
							</td>
						</tr>
						<tr>
							<td><span title="level">层级</span>：</td>
							<td><input name="level" class="mini-spinner" minValue="1"
								maxValue="3" required="true" style="width:100%;" />
							</td>
						</tr>
					</table>
				</form>
				<!--footer-->
				<div property="footer" style="text-align:center;padding:5px;">
					<a class="mini-button" onclick="onSubmit">保存</a> <a
						class="mini-button" onclick="onReset">重置</a>
				</div>
			</div>
		</div>
	</div>
</div>
<div id="win2" class="mini-window" title="修改权限" iconCls="icon2-node"
	showFooter="true" showToolbar="true" showModal="true"
	style="width:400px;height:450px;" bodyStyle="padding:0">
	<!--toolbar-->
	<div property="toolbar" style="text-align:center;padding:5px;">
		复制其他角色的权限：<input class="mini-treeselect" id="readRole"
			textfield="title" popupwidth="200"
			url="${path}/admin/rbac/role_getDataJson.aspx" />
	</div>
	<!--body-->
	<div id="treegrid2" class="mini-treegrid"
		style="width:100%;height:100%;" borderStyle="border:none"
		url="${path}/admin/rbac/node_getDataJson.aspx" showTreeIcon="true"
		treeColumn="name" expandOnLoad="true" resultAsTree="false"
		sortMode="client" allowMoveColumn="false"
		ondrawcell="onTreegrid2DrawCell" checkRecursive="false"
		autoCheckParent="true" showCheckBox="true">
		<div property="columns">
			<div field="id" width="60" headerAlign="center" align="center"
				allowSort="true" dataType="int">ID</div>
			<div name="name" field="name" width="150" headerAlign="center"
				allowSort="true">名称</div>
			<div field="title" width="100%" headerAlign="center" allowSort="true">描述</div>
		</div>
	</div>
	<!--footer-->
	<div property="footer" style="text-align:center;padding:5px;">
		<a class="mini-button" onclick="onSubmitRoleNode">保存权限</a>
	</div>
</div>

<script type="text/javascript">
mini.parse();
var treegrid1 = mini.get('treegrid1');
var panel1 = mini.get('panel1');
var form = new mini.Form('form1');
var win2 = mini.get('win2');
var treegrid2 = mini.get('treegrid2');

treegrid1.sortBy('sort','asc');

var readRole=mini.get('readRole');

readRole.on('nodeclick',function(e){
    var _roleId=e.node.id;
   
    $.ajax({
        url: '${path}/admin/rbac/role_getNodeByRoleid.aspx',
        data:{id:_roleId},
        type: 'post',
        success: function (text) {
            if(text!=''){
                treegrid2.setValue(text,false);
            }else{
                mini.alert('该角色尚未配置权限！');
            }
            treegrid2.unmask();
        },
        error: function () {
            mini.alert('权限加载失败！');
        }
    });
});

function onNew(){
    CommonPanel(panel1,'new',form,treegrid1);
    
    var _title=panel2.getTitle();
    _title=_title.split(':');
    _title=_title[0];
    panel2.setTitle(_title);
    panel2.setEnabled(false);
    treegrid2.uncheckAllNodes();
}

function onEdit(){
    CommonPanel(panel1,'edit',form,treegrid1);
}

function onDelete(){
    var _row = treegrid1.getSelected();
    if(treegrid1.getChildNodes(_row).length==0){
        CommonDelete(treegrid1,'id','${path}/admin/rbac/role_delete.aspx');
    }else{
        mini.alert('删除父级前，请先删除子级！');
    }
}

function onSubmit(){

    CommonFormSubmit(form,'${path}/admin/rbac/role_save.aspx',treegrid1);
}

function onReset(){
    CommonFormReset(form);
}


function onTreegrid1DrawCell(e){
    var _node=e.node;
    if (_node.pid == 0) {
        e.iconCls = 'icon2-role-group';
    }else{
        e.iconCls = 'icon2-role';
    }
}
function onTreegrid2DrawCell(e){
    var _node=e.node;
    if (_node.level == 1) {
        e.iconCls = 'icon2-node-module';
    }else{
        e.iconCls = 'icon2-node-action';
    }
}

function onTreegrid1RowDblClick(){
    onEdit();
}

function onBindNode(){
    readRole.setValue('');
    var _row = treegrid1.getSelected();
    if(!_row) {
        mini.alert('请选择角色');
        return;
    }
   
    var _roleId=_row.id;
    
    win2.show();
    treegrid2.reload();
    treegrid2.uncheckAllNodes();
    treegrid2.loading();
    
    $.ajax({
        url: '${path}/admin/rbac/role_getNodeByRoleid.aspx',
        data:{id:_roleId},
        type: 'post',
        success: function (text) {
            if(text!=''){
                treegrid2.setValue(text,false);
            }else{
                mini.alert('权限尚未配置！');
            }
            treegrid2.unmask();
        },
        error: function () {
            mini.alert('权限树加载失败！');
        }
    });
}

function onSubmitRoleNode(){
    var _row = treegrid1.getSelected();
    if(!_row) {
        mini.alert('尚未选择角色或新建后修改权限！');
        return;
    }
    var _roleId=_row.id;
    var _node=treegrid2.getValue();
    $.ajax({
        url: '${path}/admin/rbac/role_bindNode.aspx',
        data:{id:_roleId,node:_node},
        type: 'post',
        success: function (text) {
            if(text=='y'){
                mini.alert('保存权限成功！');
                win2.hide();
            }else{
                mini.alert(text,'保存权限失败！');
            }
            treegrid2.unmask();
        },
        error: function () {
            mini.alert('保存权限失败！');
        }
    });
}

</script>
</body>
</html>